package com.niit.search.service.impl;

import com.niit.search.entity.Topic;
import com.niit.search.mapper.TopicMapper;
import com.niit.search.service.TopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    
    private static final Logger logger = LoggerFactory.getLogger(TopicServiceImpl.class);
    
    @Autowired
    private TopicMapper topicMapper;
    
    @Override
    public List<Topic> searchTopics(String keyword) {
        logger.info("开始搜索话题，关键词: {}", keyword);
        
        if (keyword == null || keyword.trim().isEmpty()) {
            logger.warn("搜索关键词为空，返回空列表");
            return java.util.Collections.emptyList(); // 返回空列表
        }
        
        try {
            List<Topic> topics = topicMapper.searchTopicsByKeyword(keyword);
            logger.info("话题搜索结果数量: {}", topics.size());
            return topics;
        } catch (Exception e) {
            logger.error("搜索话题时发生错误: {}", e.getMessage(), e);
            return java.util.Collections.emptyList(); // 发生错误时返回空列表
        }
    }
}